package TTEST;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import Files.File_;

public class TESTXML {

	static Map<String, String> mapa = new HashMap<String, String>();
	static Set<String> set_ = new TreeSet<String>();
	/**
	 * check value that where is any number char
	 * @param value
	 * @return
	 */
	private static boolean checkvalue(String value){
		boolean g=false;
		String EXAMPLE_TEST = value;
		    Pattern pattern = Pattern.compile("([a-z]|[A-Z])*|([#]{1}|[-]){1,}");
		    //dsdsdsds#
		    Matcher matcher = pattern.matcher(EXAMPLE_TEST);
		    while (matcher.find()) {
		     // System.out.print("Start index: " + matcher.start());
		     // System.out.print(" End index: " + matcher.end() + " ");
		      System.out.print(matcher.group());
		      return true;
		    }
		    Pattern replace = Pattern.compile("\\s+");
		    Matcher matcher2 = replace.matcher(EXAMPLE_TEST);
		  //  System.out.println(matcher2.replaceAll("\t"));
		    return false;
	}
	public static Map<String, String> czytajPlikXmlZParametrami(String aa) {
		try {
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(new File("c:/LOGSDIRECTORY/" + aa+".xml" ));
			doc.getDocumentElement().normalize();
			NodeList listOfPersons = doc.getElementsByTagName("entry");
			int totalPersons = listOfPersons.getLength();
			for (int s = 0; s < listOfPersons.getLength(); s++) {
				Node firstPersonNode = listOfPersons.item(s);
				if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {
					Element firstPersonElement = (Element) firstPersonNode;
					NodeList firstNameList = firstPersonElement
							.getElementsByTagName("key");
					Element firstNameElement = (Element) firstNameList.item(0);
					NodeList textFNList = firstNameElement.getChildNodes();
					String a = ((Node) textFNList.item(0)).getNodeValue()
							.trim();
					NodeList lastNameList = firstPersonElement
							.getElementsByTagName("value");
					Element lastNameElement = (Element) lastNameList.item(0);
					NodeList textLNList = lastNameElement.getChildNodes();
					String b = ((Node) textLNList.item(0)).getNodeValue()
							.trim();
					System.out.println(checkvalue(a));
					mapa.put(a, b);
					set_.add(a + "|" + b);
				}// end of if clause
			}// end of for loop with s var
				//System.out.println("MAPA"+mapa);
				// System.out.println(set_);
			return mapa;
		} catch (SAXParseException err) {
			System.out.println("** Parsing error" + ", line "
					+ err.getLineNumber() + ", uri " + err.getSystemId());
			System.out.println(" " + err.getMessage());
			return null;
		} catch (SAXException e) {
			Exception x = e.getException();
			((x == null) ? e : x).printStackTrace();
			return null;
		} catch (Throwable t) {
			t.printStackTrace();
			return null;
		}
		// System.exit (0);
	}

	public static Map<String, String> e1(String ab) {
		try {
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			String h = "c:\\Provision.xml";
			Document doc = docBuilder.parse(new File(h));
			// normalize text representation
			doc.getDocumentElement().normalize();
			System.out.println("Root element of the doc is "
					+ doc.getDocumentElement().getNodeName());
			NodeList listOfPersons = doc.getElementsByTagName("entry");
			int totalPersons = listOfPersons.getLength();
			System.out.println("Total no of people : " + totalPersons);
			for (int s = 0; s < listOfPersons.getLength(); s++) {
				Node firstPersonNode = listOfPersons.item(s);
				if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {
					Element firstPersonElement = (Element) firstPersonNode;
					// -------
					NodeList firstNameList = firstPersonElement
							.getElementsByTagName("key");
					Element firstNameElement = (Element) firstNameList.item(0);
					NodeList textFNList = firstNameElement.getChildNodes();
					String a = ((Node) textFNList.item(0)).getNodeValue()
							.trim();
					System.out
							.println("First Name : "
									+ ((Node) textFNList.item(0))
											.getNodeValue().trim());
					// -------
					NodeList lastNameList = firstPersonElement
							.getElementsByTagName("value");
					Element lastNameElement = (Element) lastNameList.item(0);
					NodeList textLNList = lastNameElement.getChildNodes();
					String b = ((Node) textLNList.item(0)).getNodeValue()
							.trim();
					System.out
							.println("Last Name : "
									+ ((Node) textLNList.item(0))
											.getNodeValue().trim());
					mapa.put(a, b);
					set_.add(a + "|" + b);
				}// end of if clause
			}// end of for loop with s var
			System.out.println(mapa);
			System.out.println(set_);
			return mapa;
		} catch (SAXParseException err) {
			System.out.println("** Parsing error" + ", line "
					+ err.getLineNumber() + ", uri " + err.getSystemId());
			System.out.println(" " + err.getMessage());
			return null;
		} catch (SAXException e) {
			Exception x = e.getException();
			((x == null) ? e : x).printStackTrace();
			return null;
		} catch (Throwable t) {
			t.printStackTrace();
			return null;
		}
		// System.exit (0);
	}

	public static void main(String argv[]) {
		try {
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(new File("c:\\Provision.xml"));
			// normalize text representation
			doc.getDocumentElement().normalize();
			System.out.println("Root element of the doc is "
					+ doc.getDocumentElement().getNodeName());
			NodeList listOfPersons = doc.getElementsByTagName("entry");
			int totalPersons = listOfPersons.getLength();
			System.out.println("Total no of people : " + totalPersons);
			for (int s = 0; s < listOfPersons.getLength(); s++) {
				Node firstPersonNode = listOfPersons.item(s);
				if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {
					Element firstPersonElement = (Element) firstPersonNode;
					// -------
					NodeList firstNameList = firstPersonElement
							.getElementsByTagName("key");
					Element firstNameElement = (Element) firstNameList.item(0);
					NodeList textFNList = firstNameElement.getChildNodes();
					String a = ((Node) textFNList.item(0)).getNodeValue()
							.trim();
					System.out
							.println("First Name : "
									+ ((Node) textFNList.item(0))
											.getNodeValue().trim());
					// -------
					NodeList lastNameList = firstPersonElement
							.getElementsByTagName("value");
					Element lastNameElement = (Element) lastNameList.item(0);
					NodeList textLNList = lastNameElement.getChildNodes();
					String b = ((Node) textLNList.item(0)).getNodeValue()
							.trim();
					System.out
							.println("Last Name : "
									+ ((Node) textLNList.item(0))
											.getNodeValue().trim());
					mapa.put(a, b);
					set_.add(a + "|" + b);
				}// end of if clause
			}// end of for loop with s var
			System.out.println(mapa);
			System.out.println(set_);
		} catch (SAXParseException err) {
			System.out.println("** Parsing error" + ", line "
					+ err.getLineNumber() + ", uri " + err.getSystemId());
			System.out.println(" " + err.getMessage());
		} catch (SAXException e) {
			Exception x = e.getException();
			((x == null) ? e : x).printStackTrace();
		} catch (Throwable t) {
			t.printStackTrace();
		}
		// System.exit (0);
	}

	private static String getValue(String tag, Element element) {
		NodeList nodes = element.getElementsByTagName(tag).item(0)
				.getChildNodes();
		Node node = (Node) nodes.item(0);
		return node.getNodeValue();
	}

	/*
	 * _UPLOAD u=new _UPLOAD(); try { File file = new File("C:\\_UPLOAD.xml");
	 * JAXBContext jaxbContext = JAXBContext.newInstance(_UPLOAD.class);
	 * Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
	 * jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
	 * 
	 * jaxbMarshaller.marshal(u, file); jaxbMarshaller.marshal(u, System.out);
	 * 
	 * } catch (JAXBException e) { e.printStackTrace(); }
	 */
	// }

	public static List<String> cccc(String nameofthePanel) {
		return new File_().read__(nameofthePanel);
	}
	/*
	 * static void d(){ UPLOAD o=new UPLOAD(); for (String h:o.aa) {
	 * System.out.println(h); } }
	 */
}
